//
//  JWShopMeCompleteMerchantEntryVC.m
//  jinwang
//
//  Created by iOS on 2018/4/13.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopMeCompleteMerchantEntryVC.h"

#import "JWShopMeCompleteMerchantEntryTVCell.h"
#import "JWShopMeCompleteMerchantEntryCVCell.h"
#import "JWShopMeCompleteMerchantEntryTVHeaderView.h"

#import "JWShopMeShopTimeVC.h"


static NSString *const JWShopMeCompleteMerchantEntryTVCell_ID = @"JWShopMeCompleteMerchantEntryTVCell_ID";
static NSString *const JWShopMeCompleteMerchantEntryCVCell_ID = @"JWShopMeCompleteMerchantEntryCVCell_ID";
static NSString *const JWShopMeCompleteMerchantEntryTVHeaderView_ID = @"JWShopMeCompleteMerchantEntryTVHeaderView_ID";
static NSUInteger const CVCellHieght = 40;

@interface JWShopMeCompleteMerchantEntryVC ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewHeight;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (nonatomic, strong)NSMutableArray *collectionViewDataSource;

@property (nonatomic, strong) JWShopMeCompleteMerchantEntryTVHeaderView *tableViewHeard;

@end

@implementation JWShopMeCompleteMerchantEntryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
    
    [self getCollectionViewDataSourceRequest];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
//    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
}

-(void)createUI{
    
    
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }


    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled =NO; //设置tableview 不能滚动
    

    self.tableView.bounces = NO;
    
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"JWShopMeCompleteMerchantEntryTVCell" bundle:nil] forCellReuseIdentifier:JWShopMeCompleteMerchantEntryTVCell_ID];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"JWShopMeCompleteMerchantEntryTVHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:JWShopMeCompleteMerchantEntryTVHeaderView_ID];
    
    
    
    self.tableViewHeard = [JWShopMeCompleteMerchantEntryTVHeaderView createViewFromNib];

    kWeakSelf(self);
    self.tableViewHeard.backBlock = ^{
        [weakself.navigationController popViewControllerAnimated:YES];
    };
    self.tableViewHeard.chageImageBlock = ^{
        NSLog(@"跳转图片界面");
    };
    self.tableView.tableHeaderView = self.tableViewHeard;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 1.0;//同一行内cell的间距
    layout.minimumLineSpacing = 1;//行间距
    
    CGFloat width = (SCREEN_WIDTH - 2)/3;
    
    layout.itemSize = CGSizeMake(width, CVCellHieght);
    
    self.collectionView.collectionViewLayout = layout;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"JWShopMeCompleteMerchantEntryCVCell" bundle:nil] forCellWithReuseIdentifier:JWShopMeCompleteMerchantEntryCVCell_ID];
    self.collectionView.bounces = NO;
    self.collectionView.scrollEnabled = NO;
}


#pragma tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWShopMeCompleteMerchantEntryTVCell *cell = [tableView dequeueReusableCellWithIdentifier:JWShopMeCompleteMerchantEntryTVCell_ID forIndexPath:indexPath];
    
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma collectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.collectionViewDataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *model = self.collectionViewDataSource[indexPath.row];
    
    JWShopMeCompleteMerchantEntryCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:JWShopMeCompleteMerchantEntryCVCell_ID forIndexPath:indexPath];
    cell.index = indexPath.row;
    cell.model = model;
    cell.deleteBlock = ^(NSInteger index) {
        NSLog(@"tag: %ld", index);
    };
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *model = self.collectionViewDataSource[indexPath.row];

    if ([model[@"menu_tag"] isEqualToString:@"hours"]) {
        
        JWShopMeShopTimeVC *vc = [[JWShopMeShopTimeVC alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}


-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getCollectionViewDataSourceRequest{
    

    
    [[DDNetworkManagerDate makeUrl_shop_menuListWithToken:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSArray *data = responseObject[@"data"];
            [self.collectionViewDataSource addObjectsFromArray:data];
            
            
            //130 tableviewHeader 高度, 490 tableView高度, ceil*40 cell的总高度, 5 collectionView距离底部的高度, 60 底部高度
            //tableview的高度是包含headerview的高度的
            CGFloat height = 620 + ceil((self.collectionViewDataSource.count/3.0))* CVCellHieght  + ceil((self.collectionViewDataSource.count/3.0)) + 10 + 60;
            self.bgViewHeight.constant = height;
            
            [self.collectionView reloadData];
        }
        
    } failure:^(id errorObject) {
        
        [self showBottomMessag:@"网络请求失败"];
    }];
    
}



-(NSMutableArray*)collectionViewDataSource{
    if (!_collectionViewDataSource) {
        _collectionViewDataSource = [[NSMutableArray alloc]init];
    }
    return _collectionViewDataSource;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
